package com.ssm.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ssm.pojo.User;
import com.ssm.service.IUserService;
import com.ssm.utils.ResultInfoUtil;
import com.ssm.vo.ErrorCodeConsField;

@RestController
@RequestMapping("/common/rest")
public class CommonRestController {

	@Resource
	private IUserService userService;

	@RequestMapping(value = "/showUser", method = { RequestMethod.POST, RequestMethod.GET })
	public Object toIndex(HttpServletRequest request) {
		String id = request.getParameter("id");
		System.out.println("==id==");
		System.out.println(id);
		if(null != id){
			int userId = Integer.parseInt(id);
			User user = this.userService.getUserById(userId);
			return ResultInfoUtil.setSuccessInfo(user);
		}else{
			return ResultInfoUtil.setErrorInfo(ErrorCodeConsField.ERROR);
		}
	}
	
	@RequestMapping(value = "/showUserJson", method = { RequestMethod.POST, RequestMethod.GET })
	public Object toIndexJson(HttpServletRequest request, @RequestBody String json) {
		System.out.println("==json==");
		System.out.println(json);
		JSONObject obj = JSON.parseObject(json);
		String id = obj.get("id").toString();
		System.out.println(id);
		if(null != id){
			int userId = Integer.parseInt(id);
			User user = this.userService.getUserById(userId);
			return ResultInfoUtil.setSuccessInfo(user);
		}else{
			return ResultInfoUtil.setErrorInfo(ErrorCodeConsField.ERROR);
		}
	}

}
